package com.sinopec.yelofixedreader.uhf.dialog

import android.content.DialogInterface
import android.graphics.Color
import android.graphics.Point
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sinopec.basemodule.adapter.CommonAdapter
import com.sinopec.basemodule.adapter.CustomViewHolder
import com.sinopec.basemodule.dialog.DFBase
import com.sinopec.basemodule.utils.kotlin.parseInt
import com.sinopec.yelofixedreader.uhf.R

/**
 * @author lwj
 * @ClassName: CodeSameErrorDialog
 * @Description: 数量错误弹窗
 */
class AmountErrorDialog(
    private val shouldScan: String,
    private val alreadyScan: String,
    private val list: ArrayList<String>,
    private val onDismissCall:()->Unit
) : DFBase() {

    init {
        isCancelable = false
    }

    override fun createView(): Int {
        return R.layout.dialog_amount_error
    }

    override fun initView(view: View?) {
        //应扫
        view?.findViewById<TextView>(R.id.tv_needScanCount)?.text = shouldScan
        //已经扫
        view?.findViewById<TextView>(R.id.tv_totalScanCount)?.text = alreadyScan
        view?.findViewById<TextView>(R.id.tv_total )?.text = alreadyScan
        //多扫
        val spilthScanCount: Int = alreadyScan.parseInt() - shouldScan.parseInt()
        view?.findViewById<TextView>(R.id.tv_spilthScanCount)?.text = spilthScanCount.toString()

        //确认按钮关闭
        view?.findViewById<View>(R.id.tv_confirm)?.setOnClickListener {
            dismiss()
        }

        //异常编码集合model.getErrorCodes()
        val rv_list = view?.findViewById<RecyclerView>(R.id.rv_list)
        val adapter: CommonAdapter<String> = object : CommonAdapter<String>(this@AmountErrorDialog.context
            , R.layout.item_rfid_valid, list) {
            override fun convert(holder: CustomViewHolder, s: String, position: Int) {
                val itemView = holder.itemView
                //异常编码图标
                val iv_error = holder.getView<ImageView>(R.id.iv_error)
                iv_error.visibility =  View.GONE

                //rfid code
                val tv_rfidCode_right = holder.getView<TextView>(R.id.tv_rfidCode_right)
                tv_rfidCode_right.text = s
                tv_rfidCode_right.setTextColor(Color.parseColor("#5C5D66"))
            }
        };
        rv_list!!.adapter = adapter
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        this.onDismissCall()
    }

    override fun setDialogParams(window: Window) {
        val point = Point()
        window.windowManager.defaultDisplay.getSize(point)

        //设置宽度百分比
        //#第一种
        window.setLayout(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        );

        val layoutParams = window.attributes
        //#第二种
        //#第二种
        layoutParams.width = (point.x * 0.9).toInt()
        layoutParams.height = (point.y * 0.8).toInt()

        //设置dialog位置
        layoutParams.gravity = Gravity.CENTER
        //设置dialog动画
        //layoutParams.windowAnimations = R.style.xxxx
        window.attributes = layoutParams

    }
}